<?php

class MobtenerListaValores extends CI_Model
{

    /**
     * Carga los datos del IBEX35 mediante XLPATH, los carga en un array.
     *
     */
    function obtenerLista()
    {
        /*$curl = curl_init('http://www.invertia.com/mercados/bolsa/indices/acciones.asp?idtel=ib011ibex35');*/
        $curl = curl_init('http://www.invertia.com/mercados/bolsa/indices/acciones.asp?idtel=IB011IBEX35');
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10');
        $html = curl_exec($curl);
        curl_close($curl);
        $dom = new DOMDocument();
        @$dom->loadHTML($html);

        $xpath = new DOMXPath($dom);
        $tableRows = $xpath->query("//table[@title='Acciones']//tr");
        $lista = array();

        foreach ($tableRows as $row) {
            $alltd = $xpath->query('td', $row);
            if ($alltd->length > 0) {
                /*   if (is_numeric($alltd->item(1)->textContent) && is_numeric($alltd->item(5)->textContent)
            && is_numeric($alltd->item(6)->textContent) && is_numeric($alltd->item(8)->textContent)) {*/
                if ($alltd->item(0)->textContent !== "TEF.D") {
                    $this->db->query('update empresa set valor=' . str_replace(",", ".", $alltd->item(1)->textContent) . ',
                  max= ' . str_replace(",", ".", $alltd->item(5)->textContent) . ',
                  min= ' . str_replace(",", ".", $alltd->item(6)->textContent) . ',
                  capital= ' . str_replace(".", "", $alltd->item(8)->textContent) . ',
                  hora= "' . $alltd->item(11)->textContent . '" where ticker="' . $alltd->item(0)->textContent . '"');
                    //echo $this->db->affected_rows() . $alltd->item(0)->textContent . "<br>";
                }
                // }
            }
        }
    }

}
